package net.yambal.air.trackManager.GPX
{
	import com.adobe.utils.DateUtil;
	import com.google.maps.LatLng;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	
	import flashx.textLayout.formats.Float;
	
	import net.yambal.air.util.YambalStringUtility;
	
	import spark.components.Image;

	public class WayPoint{
        public static const POINT_TYPE_WPT:String = 'wpt';
        public static const POINT_TYPE_TRK:String = 'trkpt';
        public static const POINT_TYPE_RTE:String = 'rtept';
        
		[Bindable]
		public var lat:Number;
		
		[Bindable]
		public var lon:Number;
		
		[Bindable]
		public var ele:Number;
        
        [Bindable]
        public var magvar:Number;
		
		[Bindable]
		public var time:Date;
		
		[Bindable]
        public var name:String;
		
		[Bindable]
        public var desc:String;
        public var sym:String;
        public var type:String;
        
        public var _gMarker:Marker;
        
        // Wpt, Rte, Trk の何に由来するか
        private var _pointType:String;
		
		public function WayPoint(wpt:XML, type:String){
            this._pointType = type;
            this.xml = wpt;
		}
        
        /**
        * XMLをセット、パースする
        **/
        public function set xml(wpt:XML):void{
            wpt.ignoreWhitespace = true;
            var ns:Namespace = wpt.namespace();
            
            this.lat    = YambalStringUtility.toNumver(wpt.@lat);
            this.lon    = YambalStringUtility.toNumver(wpt.@lon);
            this.ele    = YambalStringUtility.toNumver(wpt.ns::ele);
            this.magvar = YambalStringUtility.toNumver(wpt.ns::magvar);
                
            this.name   = YambalStringUtility.nullComv(wpt.ns::name);
            this.desc   = YambalStringUtility.nullComv(wpt.ns::desc);
            this.sym    = YambalStringUtility.nullComv(wpt.ns::sym);
            this.type   = YambalStringUtility.nullComv(wpt.ns::type);
            
            var timeData:String = YambalStringUtility.nullComv(wpt.ns::time);
            if(timeData){
                try{
                    this.time = DateUtil.parseW3CDTF(timeData);
                }catch(e:Error){
                    trace('35@Wpt ' + e.message);
                    this.time = null;
                }
            }

            trace(this.xml);
        }
        
        public function get xml():XML{
            var ret:XML = new XML('<root></root>');
            ret.appendChild('<' + this._pointType + '></' + this._pointType + '>');
            return ret;
        }

        // Google Map
        public function get gMarker():Marker{
            if(this._gMarker){
                return this._gMarker;
            }
            this._gMarker = new Marker(this.latlng, this.markerOption);
            return this._gMarker;
        }
        
        public function get latlng():LatLng{
            return new LatLng(this.lat, this.lon);
        }
        
        public function get markerOption():MarkerOptions{
            var iconImage:Image = new Image();
            if(this._pointType == POINT_TYPE_WPT){
                iconImage.source = "assets/marker/pt16Red.png";
                return new MarkerOptions({
                    icon: iconImage,
                    iconAlignment: MarkerOptions.ALIGN_BOTTOM | MarkerOptions.ALIGN_LEFT,
                    hasShadow: false,
                    tooltip: this.name + '/' + this.time}
                );
            }else{
                iconImage.source = "assets/marker/Circle11.png";
                return new MarkerOptions({
                    icon: iconImage,
                    iconAlignment: MarkerOptions.ALIGN_VERTICAL_CENTER | MarkerOptions.ALIGN_HORIZONTAL_CENTER,
                    hasShadow: false,
                    tooltip: this.name + '/' + this.time}
                );
            }
        }
	}
}